package com.example.hello;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

public class VideoActivity extends AppCompatActivity implements View.OnClickListener {

    private VideoView video;
    private Button    start, psuse;
    private Button            mscaleAnimation;
    private Button            maphaAnimation;
    private Button            mRotateAnimation;
    private Button            mTranslateAnimation;
    private Button            mAnimationSet;
    private Button            animationListen;
    private Button            btn_start;
    private Button            btn_stop;
    private ImageView         img_show;
    private AnimationDrawable anim;
    private Button  animator;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video);
        initView();
        anim = (AnimationDrawable) img_show.getBackground();
    }

    private void initView() {
        animator = findViewById(R.id.animator);
        animator.setOnClickListener(this);
        btn_start =  findViewById(R.id.btn_start);
        btn_stop =  findViewById(R.id.btn_stop);
        img_show =  findViewById(R.id.img_show);
        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
        animationListen = findViewById(R.id.animationListen);
        animationListen.setOnClickListener(this);
        mTranslateAnimation = findViewById(R.id.mTranslateAnimation);
        mTranslateAnimation.setOnClickListener(this);
        mscaleAnimation = findViewById(R.id.mscaleAnimation);
        mscaleAnimation.setOnClickListener(this);
        maphaAnimation = findViewById(R.id.maphaAnimation);
        maphaAnimation.setOnClickListener(this);
        mRotateAnimation = findViewById(R.id.mRotateAnimation);
        mRotateAnimation.setOnClickListener(this);
        mAnimationSet = findViewById(R.id.mAnimationSet);
        mAnimationSet.setOnClickListener(this);
        video = findViewById(R.id.video);
        start = findViewById(R.id.start);
        start.setOnClickListener(this);
        psuse = findViewById(R.id.psuse);
        psuse.setOnClickListener(this);
        video.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.zms));
        video.start();

        //监听视频的状态
        video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                video.start();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                video.start();
                break;

            case R.id.psuse:
                video.pause();
                break;
            case R.id.mscaleAnimation:
                //缩放动画
                ScaleAnimation sa = new ScaleAnimation(0, 2, 1, 1);
                //设置时间
                sa.setDuration(3000);
                //保持状态
                //sa.setFillAfter(true);
                mscaleAnimation.startAnimation(sa);
                break;
            case R.id.maphaAnimation:
                AlphaAnimation al = new AlphaAnimation(0, 5);
                al.setDuration(3000);
                maphaAnimation.startAnimation(al);
                break;
            case R.id.mRotateAnimation:
                RotateAnimation ro = new RotateAnimation(0, 360);
                ro.setDuration(3000);
                mRotateAnimation.startAnimation(ro);
                break;
            case R.id.mTranslateAnimation:
                TranslateAnimation tr = new TranslateAnimation(50, 10, 50, 10);
                tr.setDuration(3000);
                tr.setFillAfter(true);
                mTranslateAnimation.startAnimation(tr);
                break;

            case R.id.mAnimationSet:
                AnimationSet set = new AnimationSet(true);

                RotateAnimation ra1 = new RotateAnimation(0, 360, 10, 20);
                ra1.setDuration(3000);
                set.addAnimation(ra1);

                AlphaAnimation al1 = new AlphaAnimation(0, 1);
                al1.setDuration(3000);
                set.addAnimation(al1);

                mAnimationSet.startAnimation(set);
                break;
            case R.id.animationListen:
                //缩放动画
                ScaleAnimation sa1 = new ScaleAnimation(0, 2, 1, 1);
                //设置时间
                sa1.setDuration(3000);
                //保持状态
                //sa.setFillAfter(true);
                animationListen.startAnimation(sa1);
                sa1.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    //开始
                    public void onAnimationStart(Animation animation) {

                    }

                    //结束
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        Toast.makeText(VideoActivity.this, "动画结束", Toast.LENGTH_SHORT).show();
                    }

                    //重复
                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                break;
            case R.id.btn_start:
                anim.start();
                break;
            case R.id.btn_stop:
                anim.stop();
                break;
            case R.id.animator:
                Intent intent = new Intent(VideoActivity.this, AnimatorActivity.class);
                startActivity(intent);
                break;
        }
    }
}